$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        init:function () {
            this.funObj().loadWidth();

            this.listener();
        },
        listener:function () {
            var $this = this;
            //修改邮箱密码
            $('#modifyMailPassword').click(function () {
                $this.funObj().changeMailPassword();
            });
            //添加邮箱
            $('#addMail').click(function () {
                $this.funObj().addMail();
            })
        },
        funObj:function () {
            var $this = this;
            return {
                loadWidth:function() { //根据页面大小 变化 图标大小
                    var width = ($(document.body).outerWidth(true) - 40) /6.0;
                    $('#gridView .row div').each(function () {
                        $(this).attr('style','height:'+width+'px;');
                        $.parser.parse($(this)); //重新渲染easyui
                    });
                    window.onresize = function(){  //重新定义界面大小处理
                       var width = ($(document.body).outerWidth(true) - 40) /6.0;
                        $('#gridView .row div').each(function () {
                            $(this).attr('style','height:'+width+'px;');
                            $.parser.parse($(this));
                        });
                    };
                },
                changeMailPassword:function () {
                    window.top.swal.setDefaults({
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '下一步',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        progressSteps: ['1', '2']
                    });
                    var obj = {},
                        steps    = [
                            {
                                title: '百分邮箱',
                                html: '<input id="email" class="swal2-input" readonly="readonly"/>',
                                onOpen:function () {
                                    xhExtra.ajaxRequest('hrm/mine/getStaffHundredMail',{},{},function (res) {
                                        if(res.email) {
                                            $('#email',window.top.document).val(res.email);
                                        }
                                    })
                                },
                                preConfirm: function() {
                                    return new Promise(function (resolve, reject) {
                                        var email = $('#email',window.top.document).val();
                                        if (!email) {
                                            reject('邮箱不能为空！');
                                        } else {
                                            obj.email = email;
                                            resolve();
                                        }
                                    })
                                }
                            },
                            {
                                title: '输入新密码',
                                confirmButtonText: '修改',
                                html: '<input type="password" id="new_pwd" class="swal2-input" placeholder="新密码" autocomplete="false" />' +
                                '<input type="password" id="confirm_pwd" class="swal2-input" placeholder="确认密码"  autocomplete="false" >',
                                preConfirm: function() {
                                    return new Promise(function (resolve, reject) {
                                        var newPwd     = $('#new_pwd', window.top.document).val(),
                                            confirmPwd = $('#confirm_pwd', window.top.document).val();
                                        if (newPwd !== confirmPwd) {
                                            reject('密码不匹配！');
                                        } else {
                                            window.top.swal.resetDefaults();
                                            obj.password = newPwd;
                                            xhExtra.ajaxRequest('hrm/mine/changeMailPassword', obj, {}, function (res) {
                                                window.top.xhTop.showSuccess(res.msg);
                                            });
                                            // $.ajax({
                                            //     url: baseUrl + 'Index/changeSelfPwd',
                                            //     type: 'POST',
                                            //     data: obj,
                                            //     complete: function(XMLHttpRequest){
                                            //         var result = XMLHttpRequest.responseJSON;
                                            //         if(xhExtra.checkError(result)) return false;
                                            //         window.top.xhTop.showMessage({type: 'success', title: result.msg});
                                            //     }
                                            // });
                                            resolve();
                                        }
                                    })
                                }
                            }];
                    window.top.swal.queue(steps)
                        .then(function(result) {window.top.swal.resetDefaults();}, function(dismiss) {window.top.swal.resetDefaults();});
                },
                addMail:function () {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '确认',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '输入邮箱密码',
                        html: '<input type="password" id="new_pwd" class="swal2-input" placeholder="新密码" autocomplete="false" />' +
                        '<input type="password" id="confirm_pwd" class="swal2-input" placeholder="确认密码"  autocomplete="false" >',
                        preConfirm: function(explain) {
                            return new Promise(function (resolve, reject) {
                                var newPwd     = $('#new_pwd', window.top.document).val(),
                                    confirmPwd = $('#confirm_pwd', window.top.document).val();
                                if (newPwd !== confirmPwd) {
                                    reject('密码不匹配！');
                                } else {
                                    resolve(newPwd);
                                }
                            })
                        }
                    }).then(function (password) {
                        xhExtra.ajaxRequest('hrm/mine/createMail', {password:password}, {}, function (res) {
                            window.top.xhTop.showSuccess(res.msg);
                        });
                    },function (dismiss) {
                        return false;
                    });
                    // console.log('添加邮箱');
                }
            }
        }
    }
};